<template>
  <el-form
    v-if="user != null"
    ref="user"
    status-icon
    :rules="rules"
    :model="user"
    label-width="80px"
  >
    <el-form-item label="昵称" prop="roleName">
      <el-input v-model="user.roleName"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="user.email" type="email"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="tel">
      <el-input v-model="user.tel"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="user.sex">
        <el-radio :label="true">男</el-radio>
        <el-radio :label="false">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="生日">
      <el-date-picker
        v-model="user.birthday"
        align="right"
        type="date"
        placeholder="选择日期"
        value-format="yyyy-MM-dd"
        :picker-options="pickerOptions1"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="真实姓名">
      <el-input v-model="user.name"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button style="width: 320px" round @click="submit('user')" type="primary">更改信息</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "UpdateUser",
  data() {
    return {
      user: null,
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      rules: {
        roleName: [{ required: true, message: "昵称是必须的" }],
        sex: [{ required: true, message: "性别是必须的" }],
        email: [
          { required: true, message: "电子邮箱是必须的" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        tel: [
          { required: true, message: "电话是必须的", trigger: "change" },
          {
            len: 11,
            message: "请输入正确的电话号码",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  created() {
    this.user = this.$copy(this.$root.$data.user);
  },
  methods: {
    getDate() {
      let date = new Date(this.user.gmtCreate);
      return (
        date.getFullYear() +
        "-" +
        (date.getMonth() + 1) +
        "-" +
        date.getDate() +
        " " +
        date.getHours() +
        ":" +
        date.getMinutes() +
        ":" +
        date.getSeconds()
      );
    },
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$myPut(
            this,
            "/user/front/user",
            (res) => {
              this.$root.$data.user = res.data.data;
              this.user = this.$copy(this.$root.$data.user);
            },
            this.user
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
</style>